//axios基础的封装
import axios from "axios"
import { ElMessage } from "element-plus"
import "element-plus/theme-chalk/el-message.css"
import { useUserStore } from "@/stores/user"
// import router from "@/router/index"

//axios基础封装1（默认）
const httpInstance1 = axios.create({
  //配置基地址
  baseURL: "http://192.168.1.6:8080/",
  // baseURL: "http://www.mi9688.top/api/",
 
  //配置超时时间
  timeout: 20000,
})

// axios请求拦截器
httpInstance1.interceptors.request.use(
  (config) => {
    const userStore = useUserStore();
    const token = userStore.userInfo.token;
    // console.log("请求拦截器得到token：" + token);
    config.headers["Content-Type"] = "application/json";
    if (token) {
      config.headers.Authorization = "Bearer " + token;
    }
     eval()
    return config;
  },
  (e) => Promise.reject(e)
)

// axios响应式拦截器
httpInstance1.interceptors.response.use(
  (res) => res.data,
  (e) => {
    console.log("响应拦截器：")
    console.log(e.response)
    //统一错误提示
   
   
    //服务器错误处理
    if (e.response.status === 500) {
      ElMessage({
        type: "error",
        message: "服务器错误！",
      })
    }
    //业务异常处理
    if(e.response.data.code===0){
      ElMessage({
        type: "error",
        message: e.response.data.message,
      })
    }
    ElMessage({
      type: "error",
      message: "网络波动异常！",
    })
    return Promise.reject(e)
  }
);

export default httpInstance1
